package com.cssy.xxkj.logic.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cssy.authority.dao.AppSqlConfig;
import com.cssy.authority.service.IAuthorityService;
import com.cssy.authority.service.IScUserRolewhPlaceService;
import com.cssy.authority.service.impl.AuthorityServiceImpl;
import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.xxkj.logic.contants.BillStatusUtil;
import com.cssy.xxkj.logic.entity.LogicWarehouseOtheroutDO;
import com.cssy.xxkj.logic.entity.LogicWarehouseOtheroutItemDO;
import com.cssy.xxkj.logic.service.ILogicWareHouseOtherOutItemService;
import com.cssy.xxkj.logic.service.ILogicWareHouseOtherOutService;
import com.cssy.xxkj.logic.service.ILogicWarehouseInventoryService;
import com.cssy.xxkj.logic.util.OftenMapUtil;
import com.cssy.xxkj.logic.util.OftenMothedUtil;
import com.cssy.yd.logic.util.SqlUtil;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.plat.exception.SyBaseException;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.OperationFlag;
import com.plat.util.SessionUtil;
import com.plat.util.json.OutJsonGrid;
/**
 * 其他出库controller
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/logic/otherOutwh")
public class OtherOutController extends BaseController<LogicWarehouseOtheroutDO> {

	private static final String MANAGER = "/xxkj/logic/otheroutwh/Manager";
	private static final String EIDT = "/xxkj/logic/otheroutwh/Operation";// 添加、修改、显示页面
	
	
	@Resource(name = "logicWareHouseOtherOutService")
	private ILogicWareHouseOtherOutService logicWareHouseOtherOutService;

	@Resource(name = "logicWareHouseOtherOutItemService")
	private ILogicWareHouseOtherOutItemService logicWareHouseOtherOutItemService;
	
	@Resource(name = "logicWarehouseInventoryService")
	private ILogicWarehouseInventoryService logicWarehouseInventoryService;
	
	@Resource(name="scUserRolePlaceService")
	private IScUserRolewhPlaceService scUserRolePlaceService;
	
	@Resource(name = "oftenMothedUtil")
	private OftenMothedUtil oftenMothedUtil;

	@Resource(name = "oftenMapUtil")
	private OftenMapUtil oftenMapUtil;
	
	@Override
	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		model.addAttribute("wh",SqlUtil.sqlUtil.roleWH(request, scUserRolePlaceService));
		return MANAGER;
	}

	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		scs=this.oftenMothedUtil.
				getNoFinishConditions(scs, "zd003BillStatus",BillStatusUtil.OTHERWH_BILL_STATUE_FOUR);
		Map<String, Object> sqlMap = SqlUtil.sqlUtil.roleWhSqlFilter(request,scUserRolePlaceService, "zd004WhCode");
		scs.addCondition((String) sqlMap.get("option"),
				(List) sqlMap.get("roleWhList"));
		if (scs.getOrderBy() == null || "".equals(scs.getOrderBy())) {
			scs.setOrderBy("bizCreatorDate desc");
		}
		Map<String, Object> whMap = SqlUtil.sqlUtil.roleWhSqlFilter(request, scUserRolePlaceService, "zd004WhCode");
		scs.addCondition(whMap.get("option")+"",(List<String>)whMap.get("roleWhList"));
		SearchResult result = logicWareHouseOtherOutService.findListByExample(scs);
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);
	}

	@Override
	public String operationForm(HttpServletRequest request,
			@PathVariable String id, @PathVariable String flag, Model model)
			throws Exception {
		model.addAttribute("wh",SqlUtil.sqlUtil.roleWH(request, scUserRolePlaceService));
		if (OperationFlag.ADD.equals(flag)) {// 我新增加时操作
			model.addAttribute("viewflag", "add");
			model.addAttribute("xx", 1);// 标示能不能修改内容
		} else {
			LogicWarehouseOtheroutDO oi = logicWareHouseOtherOutService.findListById(id);
			if (oi.getZd003BillStatus().equals(BillStatusUtil.OTHERWH_BILL_STATUS_ONEW)) {
				model.addAttribute("xx", 1);// 标示能不能修改内容
				model.addAttribute("viewflag", "tijiao");
			} else {
				model.addAttribute("xx", 2);// 标示能不能修改内容
			}
			if (oi.getZd003BillStatus().equals(BillStatusUtil.OTHERWH_BILL_STATUE_FOUR)) {
				model.addAttribute("viewflag", "tongguo");
			}
			if (oi.getZd003BillStatus().equals(BillStatusUtil.OTHERWH_BILL_STATUE_TWO)) {
				model.addAttribute("viewflag", "shenpi");
			}
			model.addAttribute("oi", oi);
			
			if(oi!=null&&StringUtils.isNotBlank(oi.getZd006Aegnt())){
				SqlMapClient sqlMapClient = AppSqlConfig.getSqlMapInstance();
				IAuthorityService ias = new AuthorityServiceImpl(sqlMapClient);
				com.cssy.authority.entity.User u = ias.getByUserName(oi.getZd006Aegnt());
				model.addAttribute("userName", u.getRealname());
			}
		}
		return EIDT;
	}
	
	@RequestMapping("/griditems")
	public void griditems(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		if(StringUtils.isNotBlank(id)){
			List<LogicWarehouseOtheroutItemDO> loiilist = logicWareHouseOtherOutItemService.findByPid(id);
			OutJsonGrid out = new OutJsonGrid(loiilist);
			out.printJSon(response);
		}
	} 

	@Override
	public Map<String, Object> save(HttpServletRequest request,
			LogicWarehouseOtheroutDO curAP, BindingResult result, Model model)
			throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try {
			String json = request.getParameter("addressInfos");
			if(json==null||"".equals(json)){
				bodyMap.isFail();
				bodyMap.configWebInfor("请选择要出库的物品！");
				return bodyMap.modelMap;
			}
			logicWareHouseOtherOutService.outSave(curAP,request);
			bodyMap.isSuccess();
			bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
		} catch (SyBaseException e) {
			e.printStackTrace();
			bodyMap.isFail();
			bodyMap.configWebInfor(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
		return bodyMap.modelMap;
	}

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try {
			String ids = request.getParameter("ids");
			String[] fields = ids.split(",");
			for (String field : fields) {
				LogicWarehouseOtheroutDO lout=logicWareHouseOtherOutService.findListById(field);
				lout.setRemoved(1l);
				logicWareHouseOtherOutService.update(lout, SessionUtil.getUser(request));
//				logicOtherOutwhService.delete(field,
//						SessionUtil.getUser(request));
			}

			bodyMap.isSuccess(GlobalResLoader.getOpsuccess());
		} catch (Exception e) {
			e.printStackTrace();
			bodyMap.isFail(GlobalResLoader.getOpfailed() + e.getMessage());
		}
		return bodyMap.modelMap;
	}
	@RequestMapping(value = "/getStorageQuaitity")
	@ResponseBody
	public String getStorageQuaitity(HttpServletRequest request, HttpServletResponse response ,Model model)throws Exception{
		String maCode = request.getParameter("maCode");
		String whCode = request.getParameter("whCode");
		String hubCode = request.getParameter("hubCode");
		String status = request.getParameter("status");
		String batch = request.getParameter("batch");
		return logicWarehouseInventoryService.getStorageQuaitity(maCode, whCode, hubCode, status, batch)+"";
	}
	
	@RequestMapping(value = "/getWhBatch")
	@ResponseBody
	public List<String> getWhBatch(HttpServletRequest request, HttpServletResponse response ,Model model)throws Exception{
		String maCode = request.getParameter("maCode");
		String whCode = request.getParameter("whCode");
		String hubCode = request.getParameter("hubCode");
		Map<String, String> map=new HashMap<String, String>();
		if(maCode!=null&&!"".equals(maCode)){
			map.put("code", maCode);
		}
		if(whCode!=null&&!"".equals(whCode)){
			map.put("whcode", whCode);
		}
		if(hubCode!=null&&!"".equals(hubCode)){
			map.put("hubcode", hubCode);
		}
		return logicWarehouseInventoryService.findbatch(map);
	}
	
	@RequestMapping(value = "/updateState")
	@ResponseBody
	public Map<String,String> updateState(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		Map<String, String> map=new HashMap<String, String>();
		try{
			String ids = request.getParameter("rows");
			if(ids!=null&&!"".equals(ids)){
				LogicWarehouseOtheroutDO lot=logicWareHouseOtherOutService.findListById(ids);
				if(lot.getId()!=null){
					lot.setZd003BillStatus(BillStatusUtil.OTHERWH_BILL_STATUE_THREE);
					logicWareHouseOtherOutService.update(lot, SessionUtil.getUser(request));
					map.put("msg", "SUCCESS");
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			map.put("msg", "ERROR");
		}
		return map;
	}
}
